package com.myactiviti;

import com.study.UserBean;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

/**
 * 审批人参数化配置
 * uel表达式写法：
 * ${UserBean.getUserId()}:获取UserBean对象中的UserId
 * ${IdapService.findmangerEmplee(dmp)}:通过IdapService类中的findmangerEmplee返回值，参数dmp
 *
 */
public class testUel {

    /**
     * 部署流程
     */
    @Test
    public void test1(){
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = defaultProcessEngine.getRepositoryService();
//        Deployment deploy = repositoryService.createDeployment().addClasspathResource("flow/demo-uel.bpmn")
//                .name("UEL-Bean请假流程").deploy();
        Deployment deploy1 = repositoryService.createDeployment().addClasspathResource("flow/demo-bean.bpmn")
                .name("UEL-Bean请假流程").deploy();
        Deployment deploy = repositoryService.createDeployment().addClasspathResource("flow/demo-service.bpmn")
                .name("UEL-service请假流程").deploy();

        System.out.println("流程信息"+deploy.getId()+"---->"+deploy);
    }
    /**
     * 启动一个流程实例 ${assigness0}
     */
    @Test
    public void test2(){
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        RuntimeService runtimeService = defaultProcessEngine.getRuntimeService();
        //创建assginess用户信息
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("assigness0","zhangsan");
        map.put("assigness1","lisi");
        map.put("assigness2","wangwu");
        map.put("assigness3","zhaoliu");
        map.put("assigness4","hahaha");
        /**
         *  * ${UserBean.getUserId()}:获取UserBean对象中的UserId
         *  * ${IdapService.findmangerEmplee(dmp)}:通过IdapService类中的findmangerEmplee返回值，参数dmp
         *  表达式支持基础类型bean，list,array,map
         *  ${order.pice<250&&order.pice>100}
         */
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("demo-uel", map);//map为用户信息
        System.out.println(processInstance.getName());
        System.out.println(processInstance.getProcessDefinitionName());
        System.out.println("流程启动成功");
    }

    /**
     * 启动一个流程实例
     * ${UserBean.getUserId()}
     * bean对象集成Serializable
     */
    @Test
    public void test3(){

        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        RuntimeService runtimeService = defaultProcessEngine.getRuntimeService();
        Map map = new HashMap();
        UserBean bean = new UserBean();
        bean.setUserid("bean-zhangsan");
        map.put("userBean",bean);
        //创建assginess用户信息
        /**
         *  * ${UserBean.getUserId()}:获取UserBean对象中的UserId
         *  * ${IdapService.findmangerEmplee(dmp)}:通过IdapService类中的findmangerEmplee返回值，参数dmp
         *  表达式支持基础类型bean，list,array,map
         *  ${order.pice<250&&order.pice>100}
         */
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("demo-bean",map);//map为用户信息
        System.out.println(processInstance.getName());
        System.out.println(processInstance.getProcessDefinitionName());
        System.out.println("流程启动成功");
    }

    /**
     * 启动一个流程实例
     * ${demoservice.test1(dmp)}
     */
    @Test
    public void test4(){
        ProcessEngineConfiguration processEngineConfigurationFromResource = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("config/activiti.cfg.xml");
        ProcessEngine processEngine = processEngineConfigurationFromResource.buildProcessEngine();
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        RuntimeService runtimeService = defaultProcessEngine.getRuntimeService();
        Map map = new HashMap();
        map.put("dmp","zhangsan");
        //创建assginess用户信息
        /**
         *  * ${UserBean.getUserId()}:获取UserBean对象中的UserId
         *  * ${demoservice.test1(dmp)}:通过IdapService类中的findmangerEmplee返回值，参数dmp
         *  表达式支持基础类型bean，list,array,map
         *  ${order.pice<250&&order.pice>100}
         */
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("uel-service",map);//map为用户信息
        System.out.println(processInstance.getName());
        System.out.println(processInstance.getProcessDefinitionName());
        System.out.println("流程启动成功");

    }
}
